// pages/movie250/movie250.js
Page({

  data: {
    current: 0,
    top250: [],
    loadMore: false,
    lower_flag: false
  },

  onLoad: function (options) {
    this.setData({
      onling: getApp().globalData.onling,
    })
    if (this.data.onling)
      return
    wx.showLoading({
      title: '加载中',
    })
    this._get_movie250(this.data.current)
  },
  onShareAppMessage() {

  },
  // 获取top250的电影
  _get_movie250: function (cur_num) {
    var next_num = cur_num + 1
    var start_num = cur_num * 10
    var _this = this
    wx.cloud.callFunction({
      name: 'movie',
      data: {
        action: 'getMovieTop250',
        start: start_num
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        var new_data = res.result.results
        var old_subjects = _this.data.top250
        console.log('old_subjects', old_subjects)
        if (old_subjects.length > 0) {
          var new_subjects = old_subjects.concat(new_data)
          console.log('new_subjects', new_subjects)
          new_data = new_subjects
        }
        _this.setData({
          current: next_num,
          loadMore: false,
          top250: new_data
        })
      },
      fail: console.error
    });
  },
  //九宫格点击事件
  _itemClick: function (event) {
    var subject = event.currentTarget.dataset.subject
    subject=subject.replace('https://movie.douban.com/subject/','').replace('/','')
    console.log(event)
    wx.navigateTo({
      url: "../movieDetail/movieDetail?subject=" + subject,
    })
  },
  //滑动到底
  _lower: function (e) {
    console.log('到达底部')
    //先显示加载
    this.setData({
      loadMore: true,
      lower_flag: false
    })
    //延迟一定时间后请求数据
    var _this = this
    setTimeout(function () {
      _this._get_movie250(_this.data.current)
    }, 3000)
  },
})